<template>
  <div :key="updateKey" v-loading="loading">
    <wk-custom-base-detail v-if="!showData" :list="postList">
      <template slot="data" slot-scope="{ data }">{{ data.value }}</template>
      <el-button v-if="editAuth && this.loginUser.isAdmin" slot="more" type="text" @click="postCommandSelect('edit')">编辑</el-button>
    </wk-custom-base-detail>


    <create-sections title="兼职信息">
      <wk-base-detail-section v-for="(item, index) in concurrentPostList" :key="index"
        :dropdown-items="showData ? null : concurrentPostDropdownItems" :list="item"
        @command-select="commandSelect($event, index)" />
      <el-button v-if="!showData" class="base-add-button" plain @click="certificateCreateClick">+ 添加兼职信息</el-button>
    </create-sections>

    <wk-base-detail v-if="leaveList.length > 0" :dropdown-items="dropdownItems" :list="leaveList"
      @top-command-select="topCommandSelect" />

    <form-add-dialog ref="formAdddialog" :title="formAddTitle" :form.sync="formAddForm" :rules="formAddRules"
      :fields="formAddFields" :visible.sync="formAddDialogShow" @pass="formAddPass" @change="formAddChange" />

    <custom-form-add-dialog v-if="customFormAddDialogShow" :id="id" ref="customFormAdddialog" :title="formAddTitle"
      :fields="customFormAddFields" :visible.sync="customFormAddDialogShow" @pass="customFormAddPass" />
  </div>
</template>

<script>
import {
  hrmEmployeePostQueryByIdAPI,
  hrmEmployeePostHighlightQueryAPI,
  hrmEmployeePostSetLeaveAPI,
  hrmEmployeePostUpdatePostAPI,
  hrmRecruitChannelQueryAPI,
  hrmEmployeePostAddConcurrentPostAPI,
  hrmEmployeeDeleteConcurrentPostAPI
} from '@/api/hrm/employeePost'
import {
  hrmEmployeeDeleteEduAPI,
  hrmEmployeeFieldVerifyAPI,
  hrmEncryptionGroupAPI,
  queryLoginEmployeeAPI,
} from '@/api/hrm/employee'

import WkBaseDetail from '@/components/WkBaseDetail'
import WkCustomBaseDetail from '@/components/WkBaseDetail/CustomIndex'
import FormAddDialog from './FormAddDialog'
import CustomFormAddDialog from './CustomFormAddDialog'

import employeePost from '../model/employeePost'
import employField from '../model/employField'
import { objDeepCopy } from '@/utils'
import { isEmpty, isArray } from '@/utils/types'
import {
  concurrentPostModel,
  educationModel,
  employeeModel,
} from '../model/employee'
import CustomFieldsMixin from '@/mixins/CustomFields'
import CreateSections from "@/components/CreateSections";
import WkBaseDetailSection from "@/components/WkBaseDetail/WkBaseDetailSection";

export default {
  // 岗位信息
  name: 'EmployeePostInfo',
  components: {
    CreateSections,
    WkBaseDetailSection,
    WkBaseDetail,
    WkCustomBaseDetail,
    FormAddDialog,
    CustomFormAddDialog
  },
  mixins: [CustomFieldsMixin],

  inject: ['editAuth'],
  props: {
    id: [String, Number],
    search: {
      type: String,
      default: null
    },
    refresh: {
      type: Boolean,
      default: false
    },
    detail: Object
  },
  data() {
    return {
      tempUserInfo: {},
      isEncrypt: false,
      loginUser: {},
      loading: false,
      leaveDetail: null,
      postEditList: [],
      postList: [],
      leaveFields: [{
        label: '计划离职日期',
        field: 'planQuitTime',
        value: ''
      }, {
        label: '申请离职日期',
        field: 'applyQuitTime',
        value: ''
      }, {
        label: '薪资结算日期',
        field: 'salarySettlementTime',
        value: ''
      }, {
        label: '离职类型',
        field: 'quitType',
        value: ''
      }, {
        label: '离职原因',
        field: 'quitReason',
        value: ''
      }, {
        label: '备注',
        field: 'remarks',
        value: ''
      }],
      leaveList: [],
      // 弹窗添加
      formAddCommond: '',
      formAddType: '',
      formAddForm: {},
      formAddRules: {},
      formAddFields: [],
      formAddDialogShow: false,
      channelList: [], // 招聘渠道数据
      // 新自定义字段弹窗
      customFormAddFields: [],
      customFormAddDialogShow: false,
      updateKey: Date.now(),

      concurrentPostList: [],
      concurrentPostBaseList: [],
      showData: '',
    }
  },

  computed: {
    dropdownItems() {
      const temps = []
      // 入职状态 1 在职 2 待入职 3 待离职 4 离职
      if (this.editAuth && this.detail && this.detail.entryStatus == 3) {
        temps.push({
          label: '调整离职信息',
          command: 'change-leave',
          icon: ''
        })
      }
      return temps
    },

    postDropdownItems() {
      const temps = []
      if (this.editAuth) {
        temps.push({
          label: '编辑',
          command: 'edit',
          icon: ''
        })
      }
      return temps
    },

    // 事项操作
    concurrentPostDropdownItems() {
      return this.editAuth ? [{
        label: '编辑',
        command: 'edit'
      }, {
        label: '删除',
        command: 'delete'
      }] : []
    },

    // 新建编辑标题
    formAddTitle() {
      return {
        'change-leave': '调整离职信息',
        'update-post-info': '编辑岗位信息',
        'create-concurrentPost': '新建兼职信息',
        'update-concurrentPost': '编辑兼职信息'
      }[this.formAddType]
    }
  },
  watch: {
    id: function (val) {
      this.getDetail()
    },
    refresh: function (val) {
      this.getDetail()
    },
  },
  mounted() {
    this.getLoginUser()
    this.getDetail()
  },
  activated: function () {
    console.log(3)
  },
  deactivated: function () {
    console.log(4)
  },
  methods: {
    getLoginUser() {
      queryLoginEmployeeAPI()
        .then(res => {
          this.loginUser = res.data;
        })
        .catch(() => {
        })
    },
    /** *** 职（执）业资格 */
    certificateCreateClick() {
      this.formAddType = 'create-concurrentPost'
      this.formAddForm = {}
      this.formAddRules = concurrentPostModel.rules
      this.formAddFields = concurrentPostModel.fields
      this.formAddDialogShow = true
    },
    /** *** 编辑删除操作 */
    commandSelect(command, index) {
      const data = this.concurrentPostBaseList[index]
      if (command == 'delete') {
        this.$confirm('确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            hrmEmployeeDeleteConcurrentPostAPI(data.concurrentPostId).then(res => {
              this.$message.success('删除成功')
              this.concurrentPostBaseList.splice(index, 1)
              this.concurrentPostList.splice(index, 1)
            })
          })
          .catch(() => { })
      } else if (command == 'edit') {
        this.formAddType = 'update-concurrentPost'
        this.formAddForm = {
          concurrentPostId: data.concurrentPostId,
          parentDeptId: data.parentDeptId,
          deptId: data.deptId,
          entryStatus: data.entryStatus,
          post: data.post,
          entryTime: data.entryTime,
          companyAgeStartTime: data.companyAgeStartTime,
          isOccupy: data.isOccupy,
        }
        this.formAddRules = concurrentPostModel.rules
        this.formAddFields = concurrentPostModel.fields
        this.formAddDialogShow = true
      }
    },

    /**
     * 1-员工基础信息,12-员工离职信息,51-员工兼职信息,11-员工岗位信息,61-员工紧急联系人信息,3-员工教育信息,
     * 4-员工工作经历信息,13-员工职（执）业资格信息,8-员工专业技术资格信息,13-员工专业技术工人证书信息,7-员工家庭成员及主要社会关系信息
     * 对象加密
     */
    async getEncryptObj(data, employeeId) {
      if (this.search && this.search != '') {
        let response = await hrmEmployeePostHighlightQueryAPI({
          employeeId: this.id,
          queryContent: this.search
        }).then(res => {
          return res;
        }).catch(() => {
        })
        data.information = response.data.information;
        data.concurrentPostVOList = response.data.concurrentPostVOList;
        data.employeeQuitInfo = response.data.employeeQuitInfo;
      }
      // 判断当前员工是否加密
      let resData = await hrmEncryptionGroupAPI(employeeId);
      this.isEncrypt = resData.data.isEncrypt;
      let typeGroup = resData.data.fieldGroup;
      if (this.isEncrypt) {
        // 离职信息
        let info = data.employeeQuitInfo;
        if (typeGroup.hasOwnProperty(12) && info) {
          let fieldList = typeGroup[12];
          for (let j = 0; j < fieldList.length; j++) {
            info[fieldList[j]] = "*****";
          }
        }
        // 兼职信息
        let concurrentPost = data.concurrentPostVOList;
        if (typeGroup.hasOwnProperty(51) && concurrentPost) {
          let fieldList = typeGroup[51];
          for (let i = 0; i < concurrentPost.length; i++) {
            let info = concurrentPost[i];
            for (let j = 0; j < fieldList.length; j++) {
              info[fieldList[j]] = "*****";
            }
          }
        }
        // 岗位基本信息
        let postInfo = data.information;
        if (typeGroup.hasOwnProperty(11) && postInfo) {
          let fieldList = typeGroup[11];
          for (let i = 0; i < postInfo.length; i++) {
            let info = postInfo[i];
            let fieldName = info.fieldName;
            if (fieldList.findIndex(item => item == fieldName) >= 0) {
              info.fieldValueDesc = "*****";
              info.fieldValue = "*****";
            }
          }
        }
      }
    },

    /**
     * 获取基础信息
     */
    getDetail() {
      this.loading = true
      hrmEmployeePostQueryByIdAPI(this.id)
        .then(async res => {
          this.loading = false
          this.tempUserInfo = JSON.parse(JSON.stringify(res.data))
          await this.getEncryptObj(res.data, this.id);
          this.showData = res.data.information == null
          const pList = res.data.information || []
          this.postEditList = objDeepCopy(this.tempUserInfo.information || [])
          const postList = []
          pList.forEach(item => {
            item.value = item.fieldValue
            if (item.fieldName == 'employment_forms') {
              item.value = employeeModel.employmentFormsValue[item.value]
            } else if (item.fieldName == 'probation') {
              item.value = employeeModel.probationValue[item.value]
            } else if (item.fieldName == 'status') {
              item.value = employeeModel.statusValue[item.value]
            } else if (item.formType == 'user' ||
              item.formType == 'structure' ||
              item.formType == 'recruit_channel' ||
              item.formType == 'area') {
              item.value = item.fieldValueDesc
            } else if (item.fieldName == 'company_age') {
              item.value = item.fieldValueDesc // 天数格式化的展示值再这个字段
            }
            postList.push(item)
          })

          // 逻辑表单逻辑
          const assistIds = this.getFormAssistIds([postList])
          const showPostList = []
          postList.forEach(item => {
            this.getFormItemDefaultProperty(item, false)
            item.show = !assistIds.includes(item.formAssistId)
            // 表格为了展示，提前处理为编辑数据
            if (item.formType === 'detail_table') {
              if (!isEmpty(item.value)) {
                const tableValue = item.value
                if (isArray(tableValue)) {
                  tableValue.forEach(children => {
                    if (children) {
                      children.forEach(child => {
                        if (child) {
                          child.value = child.fieldValue
                        }
                      })
                    }
                  })
                }
                item.value = this.getItemValue(item, null, 'update')
              }

              if (!isArray(item.value)) {
                item.value = []
              }
            }

            if (item.show) {
              showPostList.push(item)
            }
          })

          this.postList = [{
            name: '岗位信息',
            list: showPostList
          }]

          const leaveData = res.data.employeeQuitInfo
          this.leaveDetail = this.tempUserInfo.employeeQuitInfo
          if (leaveData) {
            const leaveFields = []
            employeePost.getFields(leaveData).forEach(item => {
              const temp = {
                label: item.name
              }
              if (item.field == 'quitType') {
                if (leaveData[item.field] == "*****") {
                  temp.value = leaveData[item.field]
                }else if (leaveData["quitTypeCn"] && leaveData["quitTypeCn"].indexOf("<font color='red'>") !== -1){
                  temp.value = leaveData["quitTypeCn"]
                }else {
                  temp.value = employeePost.quitTypeValue[leaveData[item.field]]
                }
              } else if (item.field == 'quitReason') {
                if (leaveData[item.field] == "*****") {
                  temp.value = leaveData[item.field]
                }else if (leaveData["quitReasonCn"] && leaveData["quitReasonCn"].indexOf("<font color='red'>") !== -1){
                  temp.value = leaveData["quitReasonCn"]
                }else {
                  temp.value = employeePost.quitReasonValue[leaveData[item.field]]
                }
              } else {
                temp.value = leaveData[item.field]
              }

              leaveFields.push(temp)
            })
            this.leaveList = [{
              name: '离职信息',
              list: leaveFields
            }]
          } else {
            this.leaveList = []
          }
          // 兼职信息
          const concurrentList = res.data.concurrentPostVOList || []
          const concurrentPostList = []
          this.concurrentPostBaseList = this.tempUserInfo.concurrentPostVOList || []
          concurrentList.forEach(item => {
            const list = []
            concurrentPostModel.fields.forEach(field => {
              const temp = {}
              temp.label = field.name
              if (field.field === 'parentDeptId') {
                temp.value = item.companyName
              } else if (field.field === 'deptId') {
                temp.value = item.deptName
              } else {
                temp.value = item[field.field]
              }
              list.push(temp)
            })
            const temp = {
              label: '司龄'
            }
            temp.value = item.companyAge
            list.push(temp)
            concurrentPostList.push(list)
          })
          this.concurrentPostList = concurrentPostList
        })
        .catch(() => {
          this.loading = false
        })
    },

    /**
     * 编辑操作
     */
    topCommandSelect(command) {
      this.formAddCommond = command
      if (command == 'change-leave') {
        this.formAddType = 'change-leave'
        this.formAddForm = this.leaveDetail ? {
          planQuitTime: this.leaveDetail.planQuitTime,
          applyQuitTime: this.leaveDetail.applyQuitTime,
          salarySettlementTime: this.leaveDetail.salarySettlementTime,
          quitType: this.leaveDetail.quitType,
          quitReason: this.leaveDetail.quitReason,
          remarks: this.leaveDetail.remarks,
          quitInfoId: this.leaveDetail.quitInfoId
        } : {
          quitType: 1,
          quitReason: 1
        }
        this.formAddRules = employeePost.getRules(this.formAddForm)
        this.formAddFields = employeePost.getFields(this.formAddForm)
        this.formAddDialogShow = true
      }
    },

    formAddPass() {
      this.$refs.formAdddialog.loading = true
      this.formAddForm.employeeId = this.id
      let request = null
      if (this.formAddType === 'create-concurrentPost') {
        request = hrmEmployeePostAddConcurrentPostAPI
      } else if (this.formAddType === 'update-concurrentPost') {
        request = hrmEmployeePostAddConcurrentPostAPI
      } else {
        request = hrmEmployeePostSetLeaveAPI
      }

      request(this.formAddForm).then(res => {
        this.$refs.formAdddialog.loading = false
        this.$message.success(`${this.formAddTitle}成功`)
        this.formAddDialogShow = false
        this.getDetail()
        this.updateKey = Date.now()
        this.$emit('handle', { type: this.formAddCommond })
      }).catch(() => {
        this.$refs.formAdddialog.loading = false
      })
    },

    /**
     * 自定义字段保存
     */
    customFormAddPass(data) {
      const dataList = objDeepCopy(data.field)
      dataList.forEach(item => {
        if (item.fieldName == 'work_city') {
          if (item.value) {
            const keys = Object.keys(item.value)
            let address = ''
            keys.forEach(key => {
              if (item.value[key]) {
                if (!address) {
                  address = item.value[key]
                } else {
                  address += `,${item.value[key]}`
                }
              }
            })
            item.value = address
          } else {
            item.value = ''
          }
        }

        // 表格字段value 和 filed
        if (item.type === 45) {
          const tableValue = item.value
          if (isArray(tableValue)) {
            tableValue.forEach(children => {
              if (children) {
                children.forEach(child => {
                  if (child) {
                    child.fieldValue = child.value
                  }
                })
              }
            })
          }
        }

        delete item.fieldType
        item.fieldValue = item.value
        delete item.value
      })

      const dialog = this.$refs.customFormAdddialog
      dialog.loading = true
      console.log(dataList)
      const params = {}
      params.employeeId = this.id
      params.dataList = dataList

      hrmEmployeePostUpdatePostAPI(params).then(res => {
        dialog.loading = false
        this.$message.success(`${this.formAddTitle}成功`)
        this.customFormAddDialogShow = false
        this.getDetail()
        this.updateKey = Date.now()
        this.$emit('handle', { type: this.formAddCommond })
      }).catch(() => {
        dialog.loading = false
      })
    },

    /**
     * 岗位信息编辑
     */
    postCommandSelect(command) {
      if (command == 'edit') {
        const copyList = objDeepCopy(this.postEditList)
        const fields = []
        let authFields = ["join_company_time", "work_forms", "dept", "org"]
        copyList.forEach(item => {
          // 忽略掉员工状态和司龄
          if (item.fieldName != 'company_age' &&
            item.fieldName != 'become_time') {
            this.handleFieldExtendList(item)
            employField.getCreateFieldDefalutData(item)
            item.value = item.fieldValue

            this.handleTableValue(item)

            // 为和crm统一，将work_city的 formType 改为 从 area 改为 address
            if (item.fieldName === 'work_city') {
              item.formType = 'address'
            }
            // 处理部门商机为单选
            if (item.fieldName === 'dept_id' || item.fieldName === 'parent_id') {
              item.radio = true
            }
            // 1 正式  2 非正式
            if (item.fieldName == 'employment_forms') {
              item.disabled = true
              item.setting = employeeModel.employmentFormsSetting
            } else if (item.fieldName == 'probation') {
              item.setting = employeeModel.probationSetting
            } else if (item.formType == 'recruit_channel') { // 'recruit_channel' 招聘渠道
              item.formType = 'select'
              if (this.channelList && this.channelList.length > 0) {
                item.setting = this.channelList
              } else {
                this.getChannelDetail(item)
              }
            } else if (item.fieldName == 'status') {
              item.setting = employeeModel.statusSetting
            }

            // 表格为了展示，提前处理为编辑数据
            if (item.formType === 'detail_table') {
              if (!isEmpty(item.value)) {
                const tableValue = item.value
                if (isArray(tableValue)) {
                  tableValue.forEach(children => {
                    if (children) {
                      children.forEach(child => {
                        if (child) {
                          child.value = child.fieldValue
                        }
                      })
                    }
                  })
                }
              }
              if (!isArray(item.value)) {
                item.value = []
              }
            }

            // 聘用形式 是非正式 读取所有值  是正式 忽略状态的值
            // if (this.detail.employmentForms == 2 ||
            // (this.detail.employmentForms == 1 &&
            // item.fieldName != 'status')) {
            if (item.fieldName != 'status') {
              if (item.formType == 'address') {
                if (item.value) {
                  const citys = item.value.split(',')
                  item.value = {
                    province: citys[0] || '',
                    city: citys[1] || '',
                    area: citys[2] || ''
                  }
                } else {
                  item.value = {}
                }
              }
              if (this.loginUser.isSuperAdmin) {
                item.disabled = false;
              } else {
                if (authFields.includes(item.fieldName)) {
                  item.disabled = true;
                }else {
                  item.disabled = false;
                }
              }
              fields.push(item)
            }
          }
        })

        this.formAddType = 'update-post-info'
        this.customFormAddFields = fields
        this.customFormAddDialogShow = true
      }
    },

    /**
     * 验证唯一
     */
    UniquePromise({ field, value }) {
      return new Promise((resolve, reject) => {
        hrmEmployeeFieldVerifyAPI({
          fieldId: field.fieldId,
          id: this.id,
          value
        }).then(response => {
          resolve()
        }).catch(() => {
          reject()
        })
      })
    },

    formAddChange(item, index, value, valueList, editForm) {
      if (valueList) {
        item.valueList = valueList
      }
      if (item.field == 'entry_time') {
        const companyAge = this.formAddFields.find(item => item.fieldName === 'company_age_start_time')
        if (companyAge) {
          this.$set(editForm, companyAge.fieldName, editForm.entry_time)
        }
      }

      if (this.formAddType === 'change-leave' && item.field == 'quitType') {
        if (value == 1) {
          editForm.quitReason = 1
        } else if (value == 2) {
          editForm.quitReason = 11
        } else if (value == 3) {
          editForm.quitReason = ''
        }

        this.formAddRules = employeePost.getRules(editForm)
        this.formAddFields = employeePost.getFields(editForm)
      }
    },

    /**
     * 获取渠道信息
     */
    getChannelDetail(item) {
      return new Promise((resolve, reject) => {
        hrmRecruitChannelQueryAPI()
          .then(res => {
            const channelList = res.data || []
            channelList.forEach(item => {
              if (item.status == 1) {
                item.label = item.value
                item.value = item.channelId
              }
            })
            this.channelList = channelList
            this.$set(item, 'setting', this.channelList)
            resolve(this.channelList)
          })
          .catch(() => {
          })
      })
    },

    /**
     * 处理表格数据 为了转换 value 和 fieldValue
     */
    handleTableValue(item) {
      // 表格为了展示，提前处理为编辑数据
      if (item.formType === 'detail_table') {
        if (!isEmpty(item.value)) {
          const tableValue = item.value
          if (isArray(tableValue)) {
            tableValue.forEach(children => {
              if (children) {
                children.forEach(child => {
                  if (child) {
                    employField.getCreateFieldDefalutData(child)
                    child.value = child.fieldValue
                  }
                })
              }
            })
          }
        }
        if (!isArray(item.value)) {
          item.value = []
        }
      }
    },

    /**
     * 处理表格数据块数据
     */
    handleFieldExtendList(item) {
      // 表格为了展示，提前处理为编辑数据
      if (item.formType === 'detail_table') {
        if (!isEmpty(item.fieldExtendList)) {
          const fieldExtendList = item.fieldExtendList
          if (isArray(fieldExtendList)) {
            fieldExtendList.forEach(child => {
              if (child) {
                employField.getCreateFieldDefalutData(child)
                child.value = child.fieldValue
              }
            })
          }
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.base-add-button {
  width: 100%;
  padding: 11px 12px;
  margin: 10px 0;
}

.wk-base-detail-section+.wk-base-detail-section {
  border-top: 1px solid $--border-color-base;
}
</style>
